<script setup lang="ts">
import {reactive, ref} from 'vue'
import type {FormInstance, FormRules} from 'element-plus'

const ruleFormRef = ref<FormInstance>()


const ruleForm = reactive({
  email: '',
  pass: '',
  name: '',
})


const rules = reactive<FormRules<typeof ruleForm>>({
  email: [
    {required: true, message: "邮箱不能为空", trigger: 'blur'},
    {pattern:/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,message:"请填写正确的邮箱地址",trigger:'blur'}
  ],
  pass: [
      {required:true,message: "密码不能为空",trigger: 'blur'},
    {min:6,max:16,message:"密码为6-16位",trigger:'blur'}
  ],
  name: [
    {required:true,message: "用户名不能为空",trigger: 'blur'},
    {min:6,max:16,message:"用户名为6-16位",trigger:'blur'}
  ],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error submit!')
      return false
    }
  })
}

</script>

<template>
  <div class="my-container">
    <div class="pic-container animate__animated animate__fadeIn">
      <h2>HouseWork</h2>
      <img src="./counter.svg" alt="">
    </div>
    <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="120px"
        label-position="top"
    >
      <h2 class="animate__animated animate__flipInX">注册</h2>
      <el-form-item label="邮箱" prop="email">
        <el-input
            v-model="ruleForm.email"
            type="text"
            autocomplete="off"/>
      </el-form-item>
      <el-form-item label="用户名" prop="name">
        <el-input
            v-model="ruleForm.name"
            type="text"
            autocomplete="off"/>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input v-model="ruleForm.pass" type="password"
                  autocomplete="off"/>
      </el-form-item>

      <el-form-item>
        <el-button type="success" @click="submitForm(ruleFormRef)" round>注册
        </el-button
        >
        <RouterLink to="login">
          返回登录
        </RouterLink>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped lang="scss">
.my-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("wave.svg") no-repeat 0 100%;
}

.pic-container {
  width: 300px;
  margin: 30px;

  h2 {
    font-size: xx-large;
    margin-bottom: 2rem;
    text-transform: uppercase;
    color: cadetblue;
  }
}

.el-form {
  width: 300px;
  background-color: #fff;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0.1em 0.2em 0.3em rgba(0, 0, 0, 0.3);

  h2 {
    font-size: xx-large;
  }

  .el-form-item {
    margin-top: 20px;

    .el-button {
      width: 100%;
    }

    a {
      text-decoration: none;
      color: #6b6b83;
    }

    .router-link-active {
      text-decoration: none;
      color: #6b6b83;
    }
  }
}

</style>